﻿Imports Crystal.DatabaseManagement.BusinessLayer
Public Class frmDeleteLookup

    Public Event loadform()
    Delegate Sub del()
    Private _Tablename As String
    Public Property Tablename() As String
        Get
            Return _Tablename
        End Get
        Set(ByVal value As String)
            _Tablename = value
        End Set
    End Property

    Private _Lookupname As String
    Public Property Lookupname() As String
        Get
            Return _Lookupname
        End Get
        Set(ByVal value As String)
            _Lookupname = value
        End Set
    End Property

    Private _dt As DataTable
    Public Property Records() As DataTable
        Get
            Return _dt
        End Get
        Set(ByVal value As DataTable)
            _dt = value
        End Set
    End Property


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub frmDeleteLookup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Delete " + Lookupname
        grdRefTable.DataSource = Records
        hidecolumnsofgrid()
    End Sub

    ''' <summary>
    ''' this hides the unnecessary columns
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub hidecolumnsofgrid()
        Try
            If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("isdefault")) Then
                grdRefTable.DisplayLayout.Bands(0).Columns("isdefault").Hidden = True
            End If
            If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("rowid")) Then
                grdRefTable.DisplayLayout.Bands(0).Columns("rowid").Hidden = True
            End If
            If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("delete")) Then
                grdRefTable.DisplayLayout.Bands(0).Columns("delete").Hidden = True
            End If
            grdRefTable.DisplayLayout.Bands(0).Columns(0).Hidden = True
            Select Case Lookupname.ToLower
                Case "city"
                    If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("isdefault")) Then
                        grdRefTable.DisplayLayout.Bands(0).Columns("isdefault").Hidden = True
                    End If

                Case "fund type"
                    If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("Active")) Then
                        grdRefTable.DisplayLayout.Bands(0).Columns("Active").Hidden = True
                    End If
                Case "master investor type"
                    If (grdRefTable.DisplayLayout.Bands(0).Columns.Exists("Free Float Flag")) Then
                        grdRefTable.DisplayLayout.Bands(0).Columns("Free Float Flag").Hidden = True
                    End If
            End Select
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        End Try


    End Sub

    ''' <summary>
    ''' get the reference column names
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function getReferenceTableColumnInfo() As DataSet

        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTableInfo As New cMaster
        Dim dtSet As New DataSet
        Try
            eReturnVal = objReferenceTableInfo.SelectReferenceTableColumnInfo(TableName)
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            Return dtSet
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
            Return Nothing
        Finally
            eReturnVal = Nothing
            objReferenceTableInfo = Nothing
            dtSet = Nothing
        End Try
    End Function

    Private Sub delete()

        Dim objReferenceTableInfo As cMaster = Nothing
        Dim strPk As String = ""
        Dim Message As String = ""
        Dim dtSet As New DataTable
        Dim stWhere As String = ""
        Dim splitLastChar As Char()
        Dim temp As DataTable = Nothing
        Dim SystemGeneratedRecords As DataTable = Nothing
        Dim reference As DataTable = Nothing
        Dim dt As DataTable = Nothing
        Dim dv As DataView = Nothing
        Try
            temp = CType(grdRefTable.DataSource, DataTable)
            dt = CType(grdRefTable.DataSource, DataTable)
            SystemGeneratedRecords = CType(grdRefTable.DataSource, DataTable).Clone
            reference = CType(grdRefTable.DataSource, DataTable).Clone
            objReferenceTableInfo = New cMaster
            Dim ListingName As System.Text.StringBuilder = Nothing
            If (Lookupname = "Project Listing") Then
                ListingName = New System.Text.StringBuilder
                For i As Integer = 0 To temp.Rows.Count - 1
                    ListingName.Append(temp.Rows(i)("Listing Name").ToString() + ",")
                Next
                objReferenceTableInfo.DeleteListings(ListingName.ToString().Remove(ListingName.ToString().LastIndexOf(",")))
                RaiseEvent loadform()
                Me.Close()
                Return
            Else
                For i As Integer = 0 To temp.Rows.Count - 1
                    If (grdRefTable.Rows(i).Cells.Exists("isdefault")) Then
                        If (grdRefTable.Rows(i).Cells("isdefault").Text = "True") Then
                            SystemGeneratedRecords.ImportRow(temp.Rows(i))
                            'MessageBox.Show("Deletion is not allowed for system generated records.", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                            Continue For
                        End If
                    End If
                    
                    If (objReferenceTableInfo.checkbeforedelete(Lookupname.ToLower, grdRefTable.Rows(i).Cells(0).Text) = True) Then
                        dtSet = getReferenceTableColumnInfo().Tables(0)
                        stWhere = dtSet.Rows(0).Item(0).ToString()
                        strPk = grdRefTable.Rows(i).Cells(stWhere).Value
                        If strPk <> "" Then
                            splitLastChar = ","
                            strPk = strPk.TrimEnd(splitLastChar)
                            stWhere = stWhere + " IN ('" + strPk + "')"
                            If (objReferenceTableInfo.InsUpdDelReferenceTable("DELETE", Tablename.ToLower, "", stWhere) = True) Then
                                dv = New DataView(dt)
                                dv.RowFilter = dtSet.Rows(0).Item(0).ToString() + " Not IN (" + strPk + ")"
                                dt = dv.ToTable
                            End If
                        End If
                    Else
                        reference.ImportRow(temp.Rows(i))
                        'MessageBox.Show("This record cannot be deleted. It contains references in other tables.", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Next
            End If

            RaiseEvent loadform()
            grdRefTable.DataSource = dt
            If (SystemGeneratedRecords.Rows.Count <> 0) Then
                For i As Integer = 0 To SystemGeneratedRecords.Rows.Count - 1
                    For j As Integer = 0 To grdRefTable.Rows.Count - 1
                        If (grdRefTable.Rows(j).Cells(0).Value.ToString() = SystemGeneratedRecords.Rows(i).Item(0).ToString) Then
                            grdRefTable.Rows(j).Selected = True
                            Exit For
                        End If
                    Next
                Next
                lblmessage.Text = "Deletion is not allowed for system generated records."
                'MessageBox.Show("", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            If (reference.Rows.Count <> 0) Then
                For i As Integer = 0 To reference.Rows.Count - 1
                    For j As Integer = 0 To grdRefTable.Rows.Count - 1
                        If (grdRefTable.Rows(j).Cells(0).Value.ToString() = reference.Rows(i).Item(0).ToString) Then
                            grdRefTable.Rows(j).Selected = True
                            Exit For
                        End If
                    Next
                Next
                lblmessage.Text = "This record cannot be deleted. It contains references in other tables."
                '   MessageBox.Show("This record cannot be deleted. It contains references in other tables.", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If (SystemGeneratedRecords.Rows.Count = 0 AndAlso reference.Rows.Count = 0) Then
                MessageBox.Show("Record deleted successfully", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Focus()
            objReferenceTableInfo = Nothing
            dtSet = Nothing
            splitLastChar = Nothing
            temp = Nothing
            SystemGeneratedRecords = Nothing
            reference = Nothing
            dt = Nothing
            dv = Nothing
        End Try
    End Sub

    Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click
        Dim deleterecord As del = Nothing
        Try
            deleterecord = New del(AddressOf delete)
            Me.BeginInvoke(deleterecord)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            deleterecord = Nothing
        End Try

        
    End Sub

    Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
        Me.Close()
    End Sub

    Private Sub frmDeleteLookup_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.Dispose()
    End Sub
End Class